import axios from 'axios'
let baseURL = ''
if (process.env.NODE_ENV === 'development') {
    baseURL = 'http://timemeetyou.com:8889/api/private/v1/'
} else if (process.env.NODE_ENV === 'production') {
    baseURL = 'http://timemeetyou.com:8889/api/private/v1/'
}
var instance = axios.create({
    baseURL,
    timeout: 1000,
});

export function httpGet({
    url,
    
    params = {},
    
}) {
    return new Promise((resolve, reject) => {
        instance.get(url,
            {
                params,
                
            }

        ).then((res) => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
}


export function httpPost({
    url,
    data = {},
    method = 'post'
}) {
    return new Promise((resolve, reject) => {
        instance({
            url,
            data,
            method,
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}

// 请求拦截器
instance.interceptors.request.use(
    config => {
        // 每次发送请求之前判断是否存在token
        // 如果存在，则统一在http请求的header都加上token，这样后台根据token判断你的登录情况，此处token一般是用户完成登录后储存到localstorage里的
        localStorage.token && (config.headers.Authorization = localStorage.token)
        return config
    },
    error => {
        return Promise.error(error)
    })

// 响应拦截器
instance.interceptors.response.use(response => {
    return Promise.resolve(response)
}, error => {
    return Promise.reject(error.response)
})